package org.jeecg.modules.demo.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.shop.entity.VipShop;
import org.jeecg.modules.demo.shop.entity.VipShopItem;
import org.jeecg.modules.demo.shop.mapper.VipShopItemMapper;
import org.jeecg.modules.demo.shop.mapper.VipShopMapper;
import org.jeecg.modules.demo.shop.service.IVipShopService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 积分商城
 * @Author: jeecg-boot
 * @Date:   2021-07-30
 * @Version: V1.0
 */
@Service
public class VipShopServiceImpl extends ServiceImpl<VipShopMapper, VipShop> implements IVipShopService {

	@Autowired
	private VipShopMapper vipShopMapper;
	@Autowired
	private VipShopItemMapper vipShopItemMapper;

	@Override
	public IPage<Map<String, Object>> listForMini(Page<VipShop> page) {
		return vipShopMapper.selectListForMini(page);
	}

	@Override
	@Transactional
	public void saveMain(VipShop vipShop, List<VipShopItem> vipShopItemList) {
		vipShopMapper.insert(vipShop);
		if(vipShopItemList!=null && vipShopItemList.size()>0) {
			for(VipShopItem entity:vipShopItemList) {
				//外键设置
				entity.setCommodityId(vipShop.getId());
				vipShopItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(VipShop vipShop,List<VipShopItem> vipShopItemList) {
		vipShopMapper.updateById(vipShop);

		//1.先删除子表数据
		vipShopItemMapper.deleteByMainId(vipShop.getId());

		//2.子表数据重新插入
		if(vipShopItemList!=null && vipShopItemList.size()>0) {
			for(VipShopItem entity:vipShopItemList) {
				//外键设置
				entity.setCommodityId(vipShop.getId());
				vipShopItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		vipShopItemMapper.deleteByMainId(id);
		vipShopMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			vipShopItemMapper.deleteByMainId(id.toString());
			vipShopMapper.deleteById(id);
		}
	}

}
